package com.cctv.video.fraction;

import com.cctv.video.ResourceTable;
import com.cctv.video.adapter.SampleItemProvider;
import com.cctv.video.base.BaseFraction;
import com.cctv.video.bean.SampleItem;
import com.cctv.video.bean.TextBean;
import com.cctv.video.network.mvp.BaseModel;
import com.cctv.video.presenter.HomeCPresenter;
import com.cctv.video.presenter.IView.HomeCView;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 */
public class HomeFraction extends BaseFraction implements HomeCView {
    private ListContainer listContainer;

    private HomeCPresenter homeCPresenter;

    private List<String> strings = new ArrayList<>();
    private SampleItemProvider sampleItemProvider;

    @Override
    public int getUIContent() {
        return ResourceTable.Layout_fraction_home;
    }

    @Override
    public void initComponent() {
        homeCPresenter = new HomeCPresenter(this);
        initListContainer();

    }

    private void initListContainer() {
        listContainer = (ListContainer) mView.findComponentById(ResourceTable.Id_list_container);

        sampleItemProvider = new SampleItemProvider(getFractionAbility().getContext(), strings, ResourceTable.Layout_item_sample);
        listContainer.setItemProvider(sampleItemProvider);

        Button get_content = (Button) mView.findComponentById(ResourceTable.Id_get_content);
        get_content.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                homeCPresenter.getJokeList("https://autumnfish.cn/");
//                homeCPresenter.getText();
            }
        });

    }

    private ArrayList<SampleItem> getData() {
        ArrayList<SampleItem> list = new ArrayList<>();
        for (int i = 0; i <= 18; i++) {
            list.add(new SampleItem("Item" + i));
        }
        return list;
    }

    @Override
    public void onTextSuccess(BaseModel<TextBean> o) {


    }

    @Override
    public void onJokeListSuccess(BaseModel<List<String>> o) {
        strings.clear();
        strings.addAll(o.getJokes());
        sampleItemProvider.notifyDataChanged();

    }

    @Override
    public void onUploadSuccess(BaseModel<Object> o) {

    }

    @Override
    public void onDownFileSuccess(File file) {

    }


}
